package com.newland.server.service.impl;

import com.newland.server.dao.SysUserApiRepository;
import com.newland.server.dao.SysUserRepository;
import com.newland.server.model.SysUser;
import com.newland.server.model.SysUserApi;
import com.newland.server.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private SysUserApiRepository sysUserApiRepository;

    @Override
    public List<String> findApis(String principal) {
        SysUser user = sysUserRepository.findByName(principal);
        List<SysUserApi> byUserId = sysUserApiRepository.findByUserId(user.getUserId());
        ArrayList<String> list = new ArrayList<>();
        for (SysUserApi sysUserApi : byUserId) {
            list.add(sysUserApi.getApi());
        }
        return list;
    }

    @Override
    public SysUser findUser(String principal) {
        SysUser user = sysUserRepository.findByName(principal);
        return user;
    }

    @Override
    public void saveUser(SysUser user) {
        sysUserRepository.save(user);
    }
}
